# 数据处理层,连接数据库，写入数据库，读取数据库
import pymysql

host = 'localhost'
port = 3308
user = 'root'
password = '123456'
database = 'test'
charset = 'utf8'
# 连接数据库
def connect_database():
    db=pymysql.Connect(host=host, port=port, user=user, password=password, database=database, charset=charset)
    # print("Connect Successfully")
    return db

# 写入数据库
def wirte_db(sql):
    db=connect_database()
    cursor=db.cursor()
    rows=0
    try:
        rows= cursor.execute(sql)
        db.commit()
    except Exception:
        db.rollback()
        print("未能写入成功，请检查sql语句是否语法错误")
    cursor.close()
    db.close()
    return rows

# 读取数据库
def read_db(sql):
    db=connect_database()
    cursor=db.cursor()
    data=None
    try:
        cursor.execute(sql)
        data=cursor.fetchall()
    except Exception:
        print("未能读取成功，请检查sql语句是否语法错误")
    cursor.close()
    db.close()
    return data
if __name__ == '__main__':
    # pass
    # connect_database()
    name = input("请输入用户名：")
    pwd = int(input("请输入密码："))
    sql = f"select * from qiandaohu username='{name}' and password={pwd}"
    print(sql)
    # print(read_db(sql))
    # sql=f"insert into values(null,'小五',123456)"
    # print(wirte_db(sql))